# coding: utf-8
import numpy as np
import random
import cv2
import os
import math
import xml.etree.cElementTree as ET
from  mylibs.labelFile import LabelFile

class imageProcessor(object):
    def __init__(self,imgdir,imgsavedir):
        super(imageProcessor,self).__init__()
        imgdir = imgdir
        self.imgsavedir = imgsavedir
        self.img_list = []
        files = os.listdir(imgdir)
        for f in files:
            if f.split('.')[-1] in ['png', 'jpg', 'bmp', 'jpeg', 'ico', 'tif']:
                self.img_list.append(os.path.join(imgdir, f))
        self.datas = []
        for image_path in self.img_list:
            img_org,bboxes, imgName, labels = self.generateData(image_path)
            self.datas.append([img_org,bboxes, imgName, labels])
    def generateData(self,imgPath):
        if os.path.exists(imgPath):
            img = cv2.imread(imgPath)
            xmlpath =imgPath[:-4]+'.xml'
            bboxes,imgName,labels = self.readAnnotations(xmlpath)
            return img,bboxes,imgName,labels
        else:
            return None
    def changebboxestype(self,bboxes):
        for i in range(len(bboxes)):
            bboxes[i][-1]=i
        return bboxes
    def saveImage(self,img,imgname):
        imgna = self.imgName.split('.')
        imgpath = os.path.join(self.imgsavedir,imgna[0]+'_{}.{}'.format(imgname,imgna[1]))
        cv2.imwrite(imgpath, img)
    def bboxesLabel(self,img,bboxes,labels):
        img = np.array(img)
        img = img.astype(np.uint8)
        bboxes = bboxes.tolist()
        for i in range(len(bboxes)):
            bboxes[i][-1] = labels[str(bboxes[i][-1])]
        return img,bboxes
    def random_translate(self,img, bboxes,imgName,labels,isSave=True, p=1):

        if random.random() < p:
            h_img, w_img, _ = img.shape
            bboxes=np.array(bboxes)
            max_bbox = np.concatenate([np.min(bboxes[:, 0:2], axis=0), np.max(bboxes[:, 2:4], axis=0)], axis=-1)
            max_l_trans = max_bbox[0]
            max_u_trans = max_bbox[1]
            max_r_trans = w_img - max_bbox[2]
            max_d_trans = h_img - max_bbox[3]
            tx = random.uniform(-(max_l_trans - 1), (max_r_trans - 1))
            ty = random.uniform(-(max_u_trans - 1), (max_d_trans - 1))
            M = np.array([[1, 0, tx], [0, 1, ty]])
            img = cv2.warpAffine(img, M, (w_img, h_img))
            bboxes[:, [0, 2]] = bboxes[:, [0, 2]] + tx
            bboxes[:, [1, 3]] = bboxes[:, [1, 3]] + ty
        img,bboxes = self.bboxesLabel(img, bboxes,labels)
        if isSave:
            self.writeAnnatation(img, bboxes, imgName, 'tr{}'.format(round(tx,1)))
            return img,bboxes,imgName,'tr{}'.format(round(tx,1))
        else:
            bboxes = self.changebboxestype(bboxes)
            return img,bboxes,imgName[:-4]+'_tr{}'.format(round(tx,1))+imgName[-4:],labels
    def random_crop(self,img, bboxes,imgName,labels,isSave=True, p=1):
        if random.random() < p:
            h_img, w_img, _ = img.shape
            bboxes = np.array(bboxes)
            max_bbox = np.concatenate([np.min(bboxes[:, 0:2], axis=0), np.max(bboxes[:, 2:4], axis=0)], axis=-1)
            max_l_trans = max_bbox[0]
            max_u_trans = max_bbox[1]
            max_r_trans = w_img - max_bbox[2]
            max_d_trans = h_img - max_bbox[3]
            crop_xmin = max(0, int(max_bbox[0] - random.uniform(0, max_l_trans)))
            crop_ymin = max(0, int(max_bbox[1] - random.uniform(0, max_u_trans)))
            crop_xmax = max(w_img, int(max_bbox[2] + random.uniform(0, max_r_trans)))
            crop_ymax = max(h_img, int(max_bbox[3] + random.uniform(0, max_d_trans)))
            img = img[crop_ymin: crop_ymax, crop_xmin: crop_xmax]
            bboxes[:, [0, 2]] = bboxes[:, [0, 2]] - crop_xmin
            bboxes[:, [1, 3]] = bboxes[:, [1, 3]] - crop_ymin

        img, bboxes = self.bboxesLabel(img, bboxes, labels)

        if isSave:
            self.writeAnnatation(img, bboxes, imgName, 'cr{}'.format(round(crop_xmin,1)))
            return img,bboxes,imgName,'cr{}'.format(round(crop_xmin,1))
        else:
            bboxes = self.changebboxestype(bboxes)
            return img, bboxes, imgName[:-4]+'_cr{}.'.format(round(crop_xmin,1))+imgName[-4:], labels
    def random_horizontal_flip(self,img, bboxes,imgName,labels,isSave=True, p=1):
        if random.random() < p:
            _, w_img, _ = img.shape
            bboxes=np.array(bboxes)
            img = img[:, ::-1, :]
            bboxes[:, [0, 2]] = w_img - bboxes[:, [2, 0]]
        img, bboxes = self.bboxesLabel(img, bboxes, labels)
        if isSave:
            self.writeAnnatation(img, bboxes, imgName, 'hf')
            return img,bboxes,imgName,'hf'
        else:
            bboxes = self.changebboxestype(bboxes)
            return img, bboxes,imgName[:-4]+'_hf'+imgName[-4:], labels
    def random_vertical_flip(self,img, bboxes,imgName,labels,isSave=True, p=1):
        image = img.copy()
        if random.random() < p:
            h_img, _, _ = img.shape
            image = img[::-1, :, :]
            bboxes=np.array(bboxes)
            bboxes[:, [1, 3]] = h_img - bboxes[:, [3, 1]]
        image, bboxes = self.bboxesLabel(image, bboxes, labels)
        if isSave:
            self.writeAnnatation(image, bboxes, imgName, 'vf')
            return image,bboxes,imgName,'vf'
        else:
            bboxes = self.changebboxestype(bboxes)
            return image, bboxes,imgName[:-4]+'_vf'+imgName[-4:], labels
    def random_rot90_1(self,img, bboxes,imgName,labels,isSave=True, p=1):
        if random.random() < p:
            h, w, _ = img.shape
            trans_img = cv2.transpose(img)
            new_img = cv2.flip(trans_img, 1)
            bboxes = np.array(bboxes)
            if bboxes is None:
                return new_img
            else:

                bboxes[:, [0, 1, 2, 3]] = bboxes[:, [1, 0, 3, 2]]
                bboxes[:, [0, 2]] = h - bboxes[:, [0, 2]]
                new_img, bboxes = self.bboxesLabel(new_img, bboxes, labels)
                if isSave:
                    self.writeAnnatation(new_img, bboxes, imgName, 'rts')
                    return new_img,bboxes,imgName,'rts'
                else:
                    bboxes = self.changebboxestype(bboxes)
                    return new_img, bboxes,imgName[:-4]+'_rts'+imgName[-4:], labels
    def random_rot90_2(self,img, bboxes,imgName,labels,isSave=True, p=1):
        if random.random() < p:
            h, w, _ = img.shape
            trans_img = cv2.transpose(img)
            new_img = cv2.flip(trans_img, 0)
            bboxes = np.array(bboxes)
            if bboxes is None:
                return new_img
            else:
                bboxes[:, [0, 1, 2, 3]] = bboxes[:, [1, 0, 3, 2]]
                bboxes[:, [1, 3]] = w - bboxes[:, [1, 3]]
                new_img, bboxes = self.bboxesLabel(new_img, bboxes, labels)
                if isSave:
                    self.writeAnnatation(new_img, bboxes, imgName, 'rtn')
                    return new_img,bboxes,imgName,'rtn'
                else:
                    bboxes = self.changebboxestype(bboxes)
                    return new_img, bboxes,imgName[:-4]+'_rtn'+imgName[-4:], labels
    def random_bright(self,img, bboxes,imgName,labels,isSave=True, p=1, lower=0.8, upper=1.2):
        image=img.copy()
        if random.random() < p:
            mean = np.mean(img)
            image = img - mean
            bboxes = np.array(bboxes)
            value = random.uniform(lower, upper)
            image = image * value + mean * random.uniform(lower, upper)  # 亮度
            image, bboxes = self.bboxesLabel(image, bboxes, labels)
            if isSave:
                self.writeAnnatation(image, bboxes, imgName, 'br{}'.format(round(value,1)))
                return image,bboxes,imgName,'br{}'.format(round(value,1))
            else:
                bboxes = self.changebboxestype(bboxes)
                return image, bboxes,imgName[:-4]+'_br{}'.format(round(value,1))+imgName[-4:], labels
    def random_swap(self,img, bboxes,imgName,labels,isSave=True, p=1):
        image=img.copy()
        perms = ((0, 1, 2), (0, 2, 1),
                 (1, 0, 2), (1, 2, 0),
                 (2, 0, 1), (2, 1, 0))
        bboxes = np.array(bboxes)
        if random.random() < p:
            swap = perms[random.randrange(0, len(perms))]
            image[:, :, (0, 1, 2)] = img[:, :, swap]
        image, bboxes = self.bboxesLabel(image, bboxes, labels)
        if isSave:
            self.writeAnnatation(image, bboxes, imgName, 'sw{}'.format(swap))
            return image,bboxes,imgName,'sw{}'.format(swap)
        else:
            bboxes = self.changebboxestype(bboxes)
            return image, bboxes,imgName[:-4]+'_sw{}'.format(swap)+imgName[-4:], labels
    def random_saturation(self,img, bboxes,imgName,labels,isSave=True, p=0.5, lower=0.5, upper=1.5):
        value = random.uniform(lower, upper)
        image = img.copy()
        if random.random() < p:
            image[:, :, 1] = img[:, :, 1] * value
        bboxes=np.array(bboxes)
        image, bboxes = self.bboxesLabel(image, bboxes, labels)
        if isSave:
            self.writeAnnatation(image, bboxes, imgName, 'sa{}'.format(round(value,1)))
            return image,bboxes,imgName,'sa{}'.format(round(value,1))
        else:
            bboxes = self.changebboxestype(bboxes)
            return image, bboxes,imgName[:-4]+'_sa{}'.format(round(value,1))+imgName[-4:], labels
    def random_hue(self,img, bboxes,imgName,labels,isSave=True, p=1, delta=18.0):
        bboxes = np.array(bboxes)
        image = img.copy()
        value = random.uniform(-delta, delta)
        if random.random() < p:
            image[:, :, 0] = img[:, :, 0] +value
            image[:, :, 0][image[:, :, 0] > 360.0] = img[:, :, 0][img[:, :, 0] > 360.0] - 360.0
            image[:, :, 0][image[:, :, 0] < 0.0] = img[:, :, 0][img[:, :, 0] < 0.0] + 360.0
        image, bboxes = self.bboxesLabel(image, bboxes, labels)
        if isSave:
            self.writeAnnatation(image, bboxes, imgName, 'hu{}'.format(round(value,1)))
            return image,bboxes,imgName,'hu{}'.format(delta)
        else:
            bboxes = self.changebboxestype(bboxes)
            return image, bboxes,imgName[:-4]+'_hu{}'.format(round(value,1))+imgName[-4:], labels
    def random_deform(self,img, bboxes,imgName,labels,isSave=True,p=0.5):
        w_img, h_img, _ = img.shape
        bboxes=np.array(bboxes)
        max_bbox = np.concatenate([np.min(bboxes[:, 0:2], axis=0), np.max(bboxes[:, 2:4], axis=0)], axis=-1)
        rate = 0.25
        image_sf = cv2.resize(img, (math.floor(h_img * rate), math.floor(w_img * rate)), interpolation=cv2.INTER_AREA)
        result = []
        for box in bboxes:
            xmin = box[0] * rate
            ymin = box[1] * rate
            xmax = box[2] * rate
            ymax = box[3] * rate
            label = box[4]
            result.append([math.floor(xmin), math.floor(ymin), math.floor(xmax), math.floor(ymax), int(label)])
        result = np.array(result)
        image, bboxes = self.bboxesLabel(image_sf, result, labels)
        if isSave:
            self.writeAnnatation(image, bboxes, imgName, 'de{}'.format(math.floor(rate)))
            return image,bboxes,imgName,'de{}'.format(round(rate,1))
        else:
            bboxes = self.changebboxestype(bboxes)
            return image, bboxes,imgName[:-4]+'_de{}'.format(round(rate,1))+imgName[-4:], labels
    def random_scretch(self,img, bboxes,imgName,labels,isSave=True,p=0.5):
        w_img, h_img, _ = img.shape
        bboxes=np.array(bboxes)
        max_bbox = np.concatenate([np.min(bboxes[:, 0:2], axis=0), np.max(bboxes[:, 2:4], axis=0)], axis=-1)
        rateW = random.uniform(0.5, min(2, w_img / (max_bbox[2] - max_bbox[0])))
        rateH = random.uniform(0.5, min(2, h_img / (max_bbox[3] - max_bbox[1])))
        image_sc = cv2.resize(img, (int(h_img * rateW), int(w_img * rateH)), interpolation=cv2.INTER_AREA)
        result = []
        for box in bboxes:
            xmin = box[0] * rateW
            ymin = box[1] * rateH
            xmax = box[2] * rateW
            ymax = box[3] * rateH
            label = box[4]
            result.append([math.floor(xmin), math.floor(ymin), math.floor(xmax), math.floor(ymax), int(label)])
        result = np.array(result)
        image, bboxes = self.bboxesLabel(image_sc, result, labels)
        if isSave:
            self.writeAnnatation(image, bboxes, imgName, 'sc{}'.format(round(rateW,2)))
            return image,bboxes,imgName,'sc{}'.format(round(rateW,2))
        else:
            bboxes = self.changebboxestype(bboxes)
            return image, bboxes,imgName[:-4]+'_hu{}'.format(round(rateW,2))+imgName[-4:], labels
    def random_rot(self,img, bboxes,imgName,labels,isSave=True, angle=30, center=None, scale=1.0,):
        image = img.copy()
        (h, w) = img.shape[:2]
        if center is None:
            center = (w / 2, h / 2)
        M = cv2.getRotationMatrix2D(center, angle, scale)
        if bboxes is None:
            for i in range(img.shape[2]):
                image[:, :, i] = cv2.warpAffine(img[:, :, i], M, (w, h), flags=cv2.INTER_CUBIC,
                                                borderMode=cv2.BORDER_CONSTANT)
            return image,bboxes,imgName,labels
        else:
            box_x, box_y, box_label, box_tmp = [], [], [], []
            for box in bboxes:
                box_x.append(int(box[0]))
                box_x.append(int(box[2]))
                box_y.append(int(box[1]))
                box_y.append(int(box[3]))
                box_label.append(labels[str(box[-1])])
            box_tmp.append(box_x)
            box_tmp.append(box_y)
            bboxes = np.array(box_tmp)

            img_rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT)

            result = []
            for i in range(len(box_label)):
                p1, p2, p3, p4 = [bboxes[0, 2 * i], bboxes[1, 2 * i]], [bboxes[0, 2 * i + 1], bboxes[1, 2 * i]], [
                    bboxes[0, 2 * i], bboxes[1, 2 * i + 1]], [bboxes[0, 2 * i + 1], bboxes[1, 2 * i + 1]]
                p_rot1 = self.getrotPoint(p1, center, -angle)
                p_rot2 = self.getrotPoint(p2, center, -angle)
                p_rot3 = self.getrotPoint(p3, center, -angle)
                p_rot4 = self.getrotPoint(p4, center, -angle)
                pxmin, pxmax = min(p_rot1[0], p_rot2[0], p_rot3[0], p_rot4[0]), max(p_rot1[0], p_rot2[0], p_rot3[0],
                                                                                    p_rot4[0])
                pymin, pymax = min(p_rot1[1], p_rot2[1], p_rot3[1], p_rot4[1]), max(p_rot1[1], p_rot2[1], p_rot3[1],
                                                                                    p_rot4[1])
                x1, y1, x2, y2 = max(0, pxmin), max(0, pymin), max(0, pxmax), max(0, pymax)
                x1, x2 = min(w, x1), min(w, x2)
                y1, y2 = min(h, y1), min(h, y2)
                one_box = [int(x1), int(y1), int(x2), int(y2), box_label[i]]
                result.append(one_box)

            if isSave:
                self.writeAnnatation(img_rotated, result, imgName, 'rt{}'.format(angle))
                return img_rotated,result,imgName,'rt{}'.format(angle)
            else:
                bboxes = self.changebboxestype(result)
                return img_rotated, bboxes,imgName[:-4]+'_rt{}'.format(angle)+imgName[-4:],labels
    def grey_scale(self,image,bboxes,imgName,labels,isSave=True):
        img_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        w_img, h_img = img_gray.shape
        bboxes = np.array(bboxes)
        flat_gray = img_gray.reshape((w_img * h_img,)).tolist()
        A = min(flat_gray)
        B = max(flat_gray)
        img = np.uint8(255 / (B - A) * (img_gray - A) + 0.5)
        img, bboxes = self.bboxesLabel(img, bboxes, labels)
        if isSave:
            self.writeAnnatation(img, bboxes, imgName, 'gr{}'.format(round(A,2)))
            return img,bboxes,imgName,'gr{}'.format(round(A,2))
        else:
            bboxes = self.changebboxestype(bboxes)
            return img, bboxes,imgName[:-4]+'_gr{}'.format(round(A,2))+imgName[-4:], labels
    def getrotPoint(self,point,centerPoint,angle):
        p_rot =[0,0]
        p_rot[0] = (point[0]-centerPoint[0])*math.cos(angle*math.pi/180)-(point[1]-centerPoint[1])*math.sin(angle*math.pi/180)+centerPoint[0]
        p_rot[1] = (point[1] - centerPoint[1]) * math.cos(angle * math.pi / 180) + (point[0] - centerPoint[0]) * math.sin(
            angle * math.pi / 180) + centerPoint[1]
        return p_rot
    def writeAnnatation(self,img,bboxes,imgName,typename):
        img = np.array(img)
        imgna = imgName[:-4]
        imgnb = imgName[-4:]
        shapes = []
        for box in bboxes:
            shape = {}
            shape["label"]=box[-1]
            shape["pose"]='1'
            shape["truncated"]='0'
            shape['difficult']='0'
            shape["points"] = [(box[0],box[1]),(box[2],box[1]),(box[0],box[3]),(box[2],box[3])]
            shapes.append(shape)

        imgPath = os.path.join(self.imgsavedir,imgna+'_{}{}'.format(typename,imgnb))
        print(imgna,imgnb,imgPath)
        cv2.imwrite(imgPath, img)
        annatationPath = os.path.join(self.imgsavedir,imgna+'_{}.xml'.format(typename))
        labelfile = LabelFile()
        labelfile.savePascalVocFormat(annatationPath,shapes,imgPath)
    def readAnnotations(self,xml_path):
        et = ET.parse(xml_path)
        element = et.getroot()
        imgName = element.findall('filename')[0].text
        element_objs = element.findall('object')
        results = []
        labels  = {}
        labelNum = 0
        for element_obj in element_objs:
            result = []
            class_name = element_obj.find('name').text
            obj_bbox = element_obj.find('bndbox')
            x1 = int(round(float(obj_bbox.find('xmin').text)))
            y1 = int(round(float(obj_bbox.find('ymin').text)))
            x2 = int(round(float(obj_bbox.find('xmax').text)))
            y2 = int(round(float(obj_bbox.find('ymax').text)))
            result.append(int(x1))
            result.append(int(y1))
            result.append(int(x2))
            result.append(int(y2))
            result.append(labelNum)
            labels[str(labelNum)] = class_name
            labelNum = labelNum + 1
            results.append(result)

        return results,imgName,labels
    def imgbboxeshow(self,img,bboxes,imgName,labels):
        img= np.array(img)
        if type(bboxes[0][-1]) is int:
            for box in bboxes:
                box[-1] = labels[str(box[-1])]
        for box in bboxes:
            cv2.rectangle(img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
            cv2.putText(img, str(box[4]), (int(box[0]), max(20, int(box[1])-5)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
        cv2.namedWindow(imgName,cv2.WINDOW_FULLSCREEN)
        cv2.imshow(imgName,img)
    def imgeprocessorFun(self, list, datas):
        a = None
        Num = 1
        angles = [45,-45]
        for data in datas:
            for j in range(len(list)):
                if list[j] == 0:
                    if j == 0:
                        if j < len(list) - 1:
                            a = self.random_vertical_flip(data[0], data[1], data[2], data[3], False)
                        else:
                            self.random_vertical_flip(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            a = self.random_vertical_flip(a[0], a[1], a[2], a[3], False)
                        else:
                            self.random_vertical_flip(a[0], a[1], a[2], a[3], True)
                elif list[j] == 1:
                    if j == 0:
                        if j < len(list) - 1:
                            a = self.random_horizontal_flip(data[0], data[1], data[2], data[3], False)
                        else:
                            a = self.random_horizontal_flip(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            a = self.random_horizontal_flip(a[0], a[1], a[2], a[3], False)
                        else:
                            self.random_horizontal_flip(a[0], a[1], a[2], a[3], True)
                elif list[j] == 2:
                    if j == 0:
                        if j < len(list) - 1:
                            for angle in angles:
                                a = self.random_rot(data[0], data[1], data[2], data[3], False, angle)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for angle in angles:
                                self.random_rot(data[0], data[1], data[2], data[3], True, angle)
                    else:
                        if j < len(list) - 1:
                            for angle in angles:
                                a = self.random_rot(a[0], a[1], a[2], a[3], False, angle)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for angle in angles:
                                self.random_rot(a[0], a[1], a[2], a[3], True, angle)
                elif list[j] == 3:
                    if j == 0:
                        if j < len(list) - 1:
                            for trN in range(Num):
                                a = self.random_translate(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for trN in range(Num):
                                self.random_translate(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for trN in range(Num):
                                a = self.random_translate(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for trN in range(Num):
                                self.random_rot(a[0], a[1], a[2], a[3], True)
                elif list[j] == 4:
                    if j == 0:
                        if j < len(list) - 1:
                            for deN in range(Num):
                                a = self.random_deform(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for deN in range(Num):

                                self.random_deform(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for deN in range(Num):
                                a = self.random_deform(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for deN in range(Num):
                                self.random_deform(a[0], a[1], a[2], a[3], True)
                elif list[j] == 5:
                    if j == 0:
                        if j < len(list) - 1:
                            for scN in range(Num):
                                a = self.random_scretch(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for scN in range(Num):
                                self.random_scretch(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for scN in range(Num):
                                a = self.random_scretch(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for scN in range(Num):
                                self.random_scretch(a[0], a[1], a[2], a[3], True)
                elif list[j] == 6:
                    if j == 0:
                        if j < len(list) - 1:
                            for huN in range(Num):
                                a = self.random_hue(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for huN in range(Num):
                                self.random_hue(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for huN in range(Num):
                                a = self.random_hue(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for huN in range(Num):
                                self.random_hue(a[0], a[1], a[2], a[3], True)
                elif list[j] == 7:
                    if j == 0:
                        if j < len(list) - 1:
                            for brN in range(Num):
                                a = self.random_bright(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for brN in range(Num):
                                self.random_bright(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for brN in range(Num):
                                a = self.random_bright(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for brN in range(Num):
                                self.random_bright(a[0], a[1], a[2], a[3], True)
                elif list[j] == 8:
                    if j == 0:
                        if j < len(list) - 1:
                            for brN in range(Num):
                                a = self.random_crop(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for crN in range(Num):
                                self.random_crop(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for crN in range(Num):
                                a = self.random_crop(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for crN in range(Num):
                                self.random_crop(a[0], a[1], a[2], a[3], True)
                elif list[j] == 9:
                    if j == 0:
                        if j < len(list) - 1:
                            for saN in range(Num):
                                a = self.random_saturation(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for saN in range(Num):
                                self.random_saturation(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for saN in range(Num):
                                a = self.random_saturation(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for saN in range(Num):
                                self.random_saturation(a[0], a[1], a[2], a[3], True)
                elif list[j] == 10:
                    if j == 0:
                        if j < len(list) - 1:
                            for swN in range(Num):
                                a = self.random_swap(data[0], data[1], data[2], data[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for swN in range(Num):
                                self.random_swap(data[0], data[1], data[2], data[3], True)
                    else:
                        if j < len(list) - 1:
                            for swN in range(Num):
                                a = self.random_swap(a[0], a[1], a[2], a[3], False)
                                list1 = list[j + 1:]
                                self.imageprocessorFun1(list1, a)
                        else:
                            for swN in range(Num):
                                self.random_swap(a[0], a[1], a[2], a[3], True)
    def imageprocessorFun1(self, list, aa):
        a = None
        Num=1
        angles = [180,0]
        if len(list) == 1:
            if list[0] == 0:
                self.random_vertical_flip(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 1:
                self.random_horizontal_flip(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 2:
                for angle in angles :
                    self.random_rot(aa[0], aa[1], aa[2], aa[3], True, angle)
            elif list[0] == 3:
                for trN in range(Num):
                    self.random_translate(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 4:
                for deN in range(Num):
                    self.random_deform(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 5:
                for scN in range(Num):
                    self.random_scretch(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 6:
                for huN in range(Num):
                    self.random_hue(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 7:
                for brN in range(Num):
                    self.random_bright(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 8:
                for crN in range(Num):
                    self.random_crop(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 9:
                for saN in range(Num):
                    self.random_saturation(aa[0], aa[1], aa[2], aa[3], True)
            elif list[0] == 10:
                for swN in range(Num):
                    self.random_swap(aa[0], aa[1], aa[2], aa[3], True)
        else:
            for j in range(len(list)):
                if list[j] == 0:
                    if j < len(list) - 1:
                        if a is None:
                            a = self.random_vertical_flip(aa[0], aa[1], aa[2], aa[3], False)
                        else:
                            a = self.random_vertical_flip(a[0], a[1], a[2], a[3], False)
                    else:
                        self.random_vertical_flip(a[0], a[1], a[2], a[3], True)
                elif list[j] == 1:
                    if j < len(list) - 1:
                        if a is None:
                            a = self.random_horizontal_flip(aa[0], aa[1], aa[2], aa[3], False)
                        else:
                            a = self.random_horizontal_flip(a[0], a[1], a[2], a[3], False)
                    else:
                        self.random_horizontal_flip(a[0], a[1], a[2], a[3], True)
                if list[j] == 2:
                    if j < len(list) - 1:
                        for angle in angles:
                            if a is None:
                                a = self.random_rot(aa[0], aa[1], aa[2], aa[3], False, angle)
                            else:
                                a = self.random_rot(a[0], a[1], a[2], a[3], False, angle)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for angle in angles:
                            self.random_rot(a[0], a[1], a[2], a[3], True, angle)
                if list[j] == 3:
                    if j < len(list) - 1:
                        for trN in range(10):
                            if a is None:
                                a = self.random_translate(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_translate(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for trN in range(Num):
                            self.random_translate(a[0], a[1], a[2], a[3], True)
                if list[j] == 4:
                    if j < len(list) - 1:
                        for deN in range(Num):
                            if a is None:
                                a = self.random_deform(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_deform(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for deN in range(Num):
                            self.random_deform(a[0], a[1], a[2], a[3], True)
                if list[j] == 5:
                    if j < len(list) - 1:
                        for scN in range(Num):
                            if a is None:
                                a = self.random_scretch(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_scretch(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for scN in range(Num):
                            self.random_scretch(a[0], a[1], a[2], a[3], True)
                if list[j] == 6:
                    if j < len(list) - 1:
                        for huN in range(Num):
                            if a is None:
                                a = self.random_hue(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_hue(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for scN in range(Num):
                            self.random_hue(a[0], a[1], a[2], a[3], True)
                if list[j] == 7:
                    if j < len(list) - 1:
                        for brN in range(Num):
                            if a is None:
                                a = self.random_bright(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_bright(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for brN in range(Num):
                            self.random_bright(a[0], a[1], a[2], a[3], True)
                if list[j] == 8:
                    if j < len(list) - 1:
                        for crN in range(Num):
                            if a is None:
                                a = self.random_crop(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_crop(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for brN in range(Num):
                            self.random_crop(a[0], a[1], a[2], a[3], True)
                if list[j] == 9:
                    if j < len(list) - 1:
                        for saN in range(Num):
                            if a is None:
                                a = self.random_saturation(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_saturation(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for saN in range(Num):
                            self.random_saturation(a[0], a[1], a[2], a[3], True)
                if list[j] == 10:
                    if j < len(list) - 1:
                        for swN in range(Num):
                            if a is None:
                                a = self.random_swap(aa[0], aa[1], aa[2], aa[3], False)
                            else:
                                a = self.random_swap(a[0], a[1], a[2], a[3], False)
                            list1 = list[j + 1:]
                            self.imageprocessorFun1(list1, a)
                    else:
                        for swN in range(Num):
                            self.random_swap(a[0], a[1], a[2], a[3], True)